<?php

class BWGModelSlideshow {
  ////////////////////////////////////////////////////////////////////////////////////////
  // Events                                                                             //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Constants                                                                          //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Variables                                                                          //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Constructor & Destructor                                                           //
  ////////////////////////////////////////////////////////////////////////////////////////
  public function __construct() {
  }
  ////////////////////////////////////////////////////////////////////////////////////////
  // Public Methods                                                                     //
  ////////////////////////////////////////////////////////////////////////////////////////
  public function insert_image_comment($image_id,$image_author,$comment)
  {
      global $wpdb;
      $date = date("Y-m-d H:i:s");
      $wpdb->insert( 
        $wpdb->prefix.'bwg_image_comment',
        array( 'id' => 'DEFAULT','image_id'=>$image_id,'name' => $image_author,'date'=>$date,'published'=>1,'comment'=>$comment), 
        array( '%d' ,'%d','%s','%s','%d','%s')
      );
  }
  function delete_comment($comment_id)
    {
      global $wpdb;
      $wpdb->delete( $wpdb->prefix.'bwg_image_comment', array( 'id' => $comment_id) );
    }
  public function get_theme_row_data($id) {
    global $wpdb;
    if ($id) {
      $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_theme WHERE id="%d"', $id));
    }
    else {      
      $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_theme WHERE default_theme="%d"', 1));
    }
    return $row;
  }
  public function get_image_row($id)
 {
   global $wpdb;
    $query=$wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_image WHERE published=1 AND id="%d"', $id);
    $row = $wpdb->get_row($query);
    return $row;
 }
 public function get_comment_rows_data($image_id) {
    global $wpdb;
    $query =$wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_image_comment WHERE image_id="%d" AND published=1 ORDER BY `id` DESC', $image_id);
    $rows = $wpdb->get_results($query);
    return $rows;
  }
  public function get_gallery_row_data($id) {
    global $wpdb;
    $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_gallery WHERE published=1 AND id="%d"', $id));
    return $row;
  }

  public function get_image_rows_data($id, $sort_by, $order_by = 'asc', $bwg) {
    global $wpdb;
    if ($sort_by == 'size' || $sort_by == 'resolution' || $sort_by == 'filename') {
      $sort_by = ' CAST(' . $sort_by . ' AS SIGNED) ';
    }
    elseif (($sort_by != 'alt') && ($sort_by != 'date') && ($sort_by != 'filetype')) {
      $sort_by = '`order`';
    }
    $row = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_image WHERE published=1 AND gallery_id="%d" ORDER BY ' . $sort_by . ' ' . $order_by, $id));
    return $row;
  }
  

  public function get_image_rows_data_custom($gallery_id, $sort_by, $order_by = 'asc') {
    global $wpdb;
    if ($sort_by == 'size' || $sort_by == 'resolution' || $sort_by == 'filename') {
      $sort_by = ' CAST(t1.' . $sort_by . ' AS SIGNED) ';
    }
    elseif (($sort_by != 'alt') && ($sort_by != 'date') && ($sort_by != 'filetype')) {
      $sort_by = 't1.`order`';
    }
    $query=$wpdb->prepare('SELECT t1.*,t2.rate,t3.user_login FROM ' . $wpdb->prefix . 'bwg_image as t1 LEFT JOIN (SELECT rate, image_id FROM ' . $wpdb->prefix . 'bwg_image_rate WHERE ip="%s") as t2 ON t1.id=t2.image_id LEFT JOIN (SELECT id,user_login FROM ' . $wpdb->prefix . 'users) as t3 ON t1.author=t3.id WHERE t1.published=1 AND t1.gallery_id="%d" ORDER BY ' . $sort_by . ' ' . $order_by, $_SERVER['REMOTE_ADDR'], $gallery_id);
    $row = $wpdb->get_results($query);
    return $row;
  }
  public function get_options_row_data() {
    global $wpdb;
    $row = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'bwg_option WHERE id="%d"', 1));
    return $row;
  }
  function get_image_location($id)
  {
    global $wpdb;
    $query=$wpdb->prepare('SELECT * FROM '.$wpdb->prefix.'image_maps WHERE image_id=%d',$id);
    $row=$wpdb->get_row($query);
    return $row;
  }
  ////////////////////////////////////////////////////////////////////////////////////////
  // Getters & Setters                                                                  //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Private Methods                                                                    //
  ////////////////////////////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////////////////////////////
  // Listeners                                                                          //
  ////////////////////////////////////////////////////////////////////////////////////////
}